<template>
  <div class="app-container">
    <!-- AI生成题目卡片 -->
    <el-card class="box-card" style="margin-bottom: 20px;">
      <div slot="header" class="clearfix">
        <span>AI生成题目</span>
      </div>
      <el-form :model="aiForm" label-width="100px" v-loading="aiLoading">
        <el-form-item label="年级：" required>
          <el-select v-model="form.gradeLevel" placeholder="年级" clearable @change="levelChange">
            <el-option v-for="item in levelEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学科：" required>
          <el-select v-model="form.subjectId" placeholder="学科">
            <el-option v-for="item in subjectFilter" :key="item.id" :value="item.id" :label="item.name+' ( '+item.levelName+' )'"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="难度：" required>
          <el-rate v-model="form.difficult" class="question-item-rate"></el-rate>
        </el-form-item>
        <el-form-item label="知识点：" required>
          <el-input v-model="aiForm.knowledgePoint" placeholder="请输入知识点，如：函数、光合作用、牛顿第一定律等"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="generateQuestion">生成题目</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="年级：" prop="gradeLevel" required>
        <el-select v-model="form.gradeLevel"   placeholder="年级"  @change="levelChange">
          <el-option v-for="item in levelEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学科：" prop="subjectId" required>
        <el-select v-model="form.subjectId" placeholder="学科" >
          <el-option v-for="item in subjectFilter" :key="item.id" :value="item.id" :label="item.name+' ( '+item.levelName+' )'"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题干：" prop="title" required>
        <el-input v-model="form.title"   @focus="inputClick(form,'title')" />
      </el-form-item>
      <el-form-item label="答案：" prop="correct" required>
        <el-input v-model="form.correct"   @focus="inputClick(form,'correct')" />
      </el-form-item>
      <el-form-item label="解析：" prop="analyze" required>
        <el-input v-model="form.analyze"  @focus="inputClick(form,'analyze')" />
      </el-form-item>
      <el-form-item label="分数：" prop="score" required>
        <el-input-number v-model="form.score" :precision="1" :step="1" :max="100"></el-input-number>
      </el-form-item>
      <el-form-item label="难度：" required>
        <el-rate v-model="form.difficult" class="question-item-rate"></el-rate>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="success" @click="showQuestion">预览</el-button>
      </el-form-item>
    </el-form>
    <el-dialog  :visible.sync="richEditor.dialogVisible"  append-to-body :close-on-click-modal="false" style="width: 100%;height: 100%"   :show-close="false" center>
      <Ueditor @ready="editorReady"/>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editorConfirm">确 定</el-button>
        <el-button @click="richEditor.dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="questionShow.dialog" style="width: 100%;height: 100%">
      <QuestionShow :qType="questionShow.qType" :question="questionShow.question" :qLoading="questionShow.loading"/>
    </el-dialog>
  </div>
</template>

<script>
import QuestionShow from '../components/Show'
import Ueditor from '@/components/Ueditor'
import { mapGetters, mapState, mapActions } from 'vuex'
import questionApi from '@/api/question'

export default {
  components: {
    Ueditor, QuestionShow
  },
  data () {
    return {
      form: {
        id: null,
        questionType: 5,
        gradeLevel: null,
        subjectId: null,
        title: '',
        items: [],
        analyze: '',
        correct: '',
        score: '',
        difficult: 0
      },
      // 添加AI表单数据
      aiForm: {
        knowledgePoint: '',
        questionType: 5 // 简答题类型为5
      },
      aiLoading: false,
      subjectFilter: null,
      formLoading: false,
      rules: {
        gradeLevel: [
          { required: true, message: '请选择年级', trigger: 'change' }
        ],
        subjectId: [
          { required: true, message: '请选择学科', trigger: 'change' }
        ],
        title: [
          { required: true, message: '请输入题干', trigger: 'blur' }
        ],
        correct: [
          { required: true, message: '请输入答案', trigger: 'blur' }
        ],
        analyze: [
          { required: true, message: '请输入解析', trigger: 'blur' }
        ],
        score: [
          { required: true, message: '请输入分数', trigger: 'blur' }
        ]
      },
      richEditor: {
        dialogVisible: false,
        object: null,
        parameterName: '',
        instance: null
      },
      questionShow: {
        qType: 0,
        dialog: false,
        question: null,
        loading: false
      }
    }
  },
  created () {
    let id = this.$route.query.id
    let _this = this
    this.initSubject(function () {
      _this.subjectFilter = _this.subjects
    })
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      questionApi.select(id).then(re => {
        _this.form = re.response
        _this.formLoading = false
      })
    }
  },
  methods: {
    editorReady (instance) {
      this.richEditor.instance = instance
      let currentContent = this.richEditor.object[this.richEditor.parameterName]
      this.richEditor.instance.setContent(currentContent)
      // 光标定位到Ueditor
      this.richEditor.instance.focus(true)
    },
    inputClick (object, parameterName) {
      this.richEditor.object = object
      this.richEditor.parameterName = parameterName
      this.richEditor.dialogVisible = true
    },
    editorConfirm () {
      let content = this.richEditor.instance.getContent()
      this.richEditor.object[this.richEditor.parameterName] = content
      this.richEditor.dialogVisible = false
    },
    submitForm () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true
          questionApi.edit(this.form).then(re => {
            if (re.code === 1) {
              _this.$message.success(re.message)
              _this.delCurrentView(_this).then(() => {
                _this.$router.push('/exam/question/list')
              })
            } else {
              _this.$message.error(re.message)
              this.formLoading = false
            }
          }).catch(e => {
            this.formLoading = false
          })
        } else {
          return false
        }
      })
    },
    resetForm () {
      let lastId = this.form.id
      this.$refs['form'].resetFields()
      this.form = {
        id: null,
        questionType: 5,
        gradeLevel: null,
        subjectId: null,
        title: '',
        items: [],
        analyze: '',
        correct: '',
        score: '',
        difficult: 0
      }
      this.form.id = lastId
    },
    levelChange () {
      this.form.subjectId = null
      this.subjectFilter = this.subjects.filter(data => data.level === this.form.gradeLevel)
    },
    showQuestion () {
      this.questionShow.dialog = true
      this.questionShow.qType = this.form.questionType
      this.questionShow.question = this.form
    },
    // 将generateQuestion方法移到methods对象内
    generateQuestion () {
      if (!this.form.gradeLevel) {
        this.$message.error('请选择年级')
        return
      }
      if (!this.form.subjectId) {
        this.$message.error('请选择学科')
        return
      }
      if (!this.aiForm.knowledgePoint) {
        this.$message.error('请输入知识点')
        return
      }
    
      this.aiLoading = true
      // 构建请求参数
      const requestData = {
        gradeLevel: this.form.gradeLevel,
        subjectId: this.form.subjectId,
        difficult: this.form.difficult,
        knowledgePoint: this.aiForm.knowledgePoint,
        questionType: this.aiForm.questionType
      }
    
      questionApi.generateByAI(requestData).then(response => {
        const res = response.response
        if (res) {
          // 填充表单数据
          this.form.title = res.title || ''
          this.form.analyze = res.analyze || ''
          this.form.correct = res.correct || ''
          this.form.score = res.score || 5
          this.$message.success('题目生成成功')
        } else {
          this.$message.error('题目生成失败')
        }
        this.aiLoading = false
      }).catch(() => {
        this.$message.error('题目生成失败')
        this.aiLoading = false
      })
    },
    ...mapActions('exam', { initSubject: 'initSubject' }),
    ...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  },
  computed: {
    ...mapGetters('enumItem', ['enumFormat']),
    ...mapState('enumItem', {
      questionTypeEnum: state => state.exam.question.typeEnum,
      levelEnum: state => state.user.levelEnum
    }),
    ...mapState('exam', { subjects: state => state.subjects })
  },
  // 添加生成题目方法
  generateQuestion () {
    if (!this.form.gradeLevel) {
      this.$message.error('请选择年级')
      return
    }
    if (!this.form.subjectId) {
      this.$message.error('请选择学科')
      return
    }
    if (!this.aiForm.knowledgePoint) {
      this.$message.error('请输入知识点')
      return
    }
  
    this.aiLoading = true
    // 构建请求参数
    const requestData = {
      gradeLevel: this.form.gradeLevel,
      subjectId: this.form.subjectId,
      difficult: this.form.difficult,
      knowledgePoint: this.aiForm.knowledgePoint,
      questionType: this.aiForm.questionType
    }
  
    questionApi.generateByAI(requestData).then(response => {
      const res = response.response
      if (res) {
        // 填充表单数据
        this.form.title = res.title || ''
        this.form.analyze = res.analyze || ''
        this.form.correct = res.correct || ''
        this.form.score = res.score || 5
        this.$message.success('题目生成成功')
      } else {
        this.$message.error('题目生成失败')
      }
      this.aiLoading = false
    }).catch(() => {
      this.$message.error('题目生成失败')
      this.aiLoading = false
    })
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  width: 100%;
  height: 100%;
}
</style>
